package com.code.wanmi.service.impl;

import com.code.wanmi.entity.wm_user.*;
import com.code.wanmi.mapper.WmSysMenuMapper;
import com.code.wanmi.mapper.WmSysRoleMapper;
import com.code.wanmi.mapper.WmSysRoleMenuMapper;
import com.code.wanmi.service.IWmSysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author 谢晓峰
 * @since 2019-12-05
 */
@Service
public class WmSysRoleServiceImpl extends ServiceImpl<WmSysRoleMapper, WmSysRole> implements IWmSysRoleService {

    @Autowired
    private WmSysRoleMapper wmSysRoleMapper;
    @Autowired
    private WmSysRoleMenuMapper wmSysRoleMenuMapper;
    @Autowired
    private WmSysMenuMapper wmSysMenuMaper;


    @Override
    public Integer addRole(WmSysRole wmSysRole) {
        return wmSysRoleMapper.addRoletwo(wmSysRole);
    }

    @Override
    public Integer insertRole(WmSysRole role, WmSysUser user) {
        return wmSysRoleMapper.insertRole(role,user);
    }

    @Override
    public Integer updateUserRole(WmSysUser user, WmSysRole role) {
        return wmSysRoleMapper.updateUserRole(user,role);
    }





    @Override
    @Transactional
    public int addUserMenu(UserDto userDto){
        String roleName= userDto.getWmSysRole().getRoleName();
        WmSysRole wmSysRole=wmSysRoleMapper.findRoleByName(roleName);//查询后得到角色对象
        long roleId=wmSysRole.getRoleId();//得到角色id
        List<WmSysMenu> wmSysMenuList= userDto.getWmSysMenuList();
        int i=0;
        WmSysRoleMenu wmSysRoleMenu=new WmSysRoleMenu();
        //添加角色权限
        for(WmSysMenu wmSysMenu:wmSysMenuList){
            String menuName=wmSysMenu.getMenuName();
            WmSysMenu wmSysMenu1=wmSysMenuMaper.findMenuByName(menuName);//查询后得到菜单权限对象
            long menuId=wmSysMenu1.getMenuId();
            wmSysRoleMenu.setMenuId(menuId);
            wmSysRoleMenu.setRoleId(roleId);
            i = wmSysRoleMenuMapper.addUserMenu(wmSysRoleMenu);
            if(i<=0){
                throw new RuntimeException();
            }
        }
        return i;
    }

    @Override
    public int updateMenu(WmSysMenu wmSysMenu) {
      return  wmSysMenuMaper.updateMenu(wmSysMenu);
    }


//    @Override
//    public int updateMenu(WmSysMenu userDto) {

//        String roleName1= userDto.getWmSysRole().getRoleName();
//        WmSysRole wmSysRole=wmSysRoleMapper.findRoleByName(roleName1);//查询后得到角色对象
//        long roleId=wmSysRole.getRoleId();//得到角色id
//        List<WmSysMenu> wmSysMenuList= userDto.getWmSysMenuList();
//        int i=0;
//        WmSysRoleMenu wmSysRoleMenu=new WmSysRoleMenu();
//        int flag=0;
//        int flag1=0;
//        for(WmSysMenu wmSysMenu:wmSysMenuList){
//            flag++;
//            String menuName=wmSysMenu.getMenuName();
//            WmSysMenu wmSysMenu1=wmSysMenuMaper.findMenuByName(menuName);//查询后得到菜单权限对象
//            //给对象赋值
//            long menuId=wmSysMenu1.getMenuId();
//            wmSysRoleMenu.setMenuId(menuId);
//            wmSysRoleMenu.setRoleId(roleId);
//
//            List<WmSysMenu> nWmSysMenuList= userDto.getNWmSysMenuList();
//            //修改角色权限
//            for(WmSysMenu nWmSysMenu:nWmSysMenuList){
//                flag1++;
//                if(flag==flag1){
                //    i=wmSysRoleMenuMapper.updateMenu(wmSysRoleMenu,nWmSysMenu.getMenuId());
//                    if(i<=0){
//                        throw new RuntimeException();
//                    }
//                    if(i<=0){
//                        return i;
//                    }
//                }
//            }
//        }
//        return i;
//    }


    @Override
    public List<WmSysRole> display() {
        return wmSysRoleMapper.getRoles();
    }

    @Override
    public List<WmSysMenu> getRoleById(long role_id) {
        return wmSysRoleMapper.getRoleById(role_id);
    }

    /**根据角色ID删除角色*/
    @Override
    public Integer deleteRoleById(Long roleId) {
        return wmSysRoleMapper.deleteRoleById(roleId);
    }

    /**根据用户ID查询角色名
     * @param uid*/
    @Override
    public Set<WmSysRole> getRoleNameByUserId(Long uid) {
        return wmSysRoleMapper.getRoleNameByUserId(uid);
    }


}
